from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.db.models import F,Q

from .myforms import StudentForm
from .models import Student
from django.shortcuts import reverse,redirect

# from django.urls.base import reverse,resolve
# Create your views here.

def index(request):
    if request.method == "GET":
        form = StudentForm()
        return render(request, 'student2.html',{"form":form})
    if request.method == "POST":
        form = StudentForm(request.POST)
        if form.is_valid():
            student = Student(**form.cleaned_data)
            student.save()
        else:
            return render(request, 'student2.html', {"form": form})
        return HttpResponse("ok")

def look(request):
    cter='1121'
    print(cter)
    return HttpResponse('跳过来了')

def hoot(request):
    git=Student.objects.filter(Q(name__startswith='李') & Q(age__gt=20)).update(age=F('age')-1)
    print(git)
    return HttpResponse('运行成功')

def index3(request,id):
    return HttpResponse()


def getpath(request):
    res=reverse('student2:look')#应用:路由名称
    res=reverse('student2:index2',args=[1,])
    res=reverse('student2:index3',kwargs={'id':1})

    print(res)
    # return render(request,'rev.html')

    return redirect(res)

def index2(request,id):

    return HttpResponse('id'+str(id))

